package com.bbzn.device.client.service.emq.order.impl.mq;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.CustomerConstant;
import com.bbzn.device.client.dataobject.Switch;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.SwitchService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.OptionalBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author wulongbo
 * @Date 2021/12/16 16:19
 * @Version 1.0
 */
@Component
public class MqSensorOpenImpl extends BaseMqSendImpl {

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private SwitchService switchService;

    @Override
    public String checkMap(String language) throws Exception {
        if (!OptionalBean.ofNullable(map.get("shed")).isPresent() &&
                !OptionalBean.ofNullable(map.get("fall")).isPresent() &&
                !OptionalBean.ofNullable(map.get("impact")).isPresent() &&
                !OptionalBean.ofNullable(map.get("temperature")).isPresent() &&
                !OptionalBean.ofNullable(map.get("environTemp")).isPresent() &&
                !OptionalBean.ofNullable(map.get("elevation")).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(CustomerConstant.CUSTOMER_ENUM, CustomerConstant.SENSOR_VALUE_NULL_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Switch aSwitch = new Switch();
        if (OptionalBean.ofNullable(map.get("shed")).isPresent()) {
            aSwitch.setDropOpen(Integer.parseInt(map.get("shed").toString()));
        }
        if (OptionalBean.ofNullable(map.get("fall")).isPresent()) {
            aSwitch.setDropOpen(Integer.parseInt(map.get("fall").toString()));
        }
        if (OptionalBean.ofNullable(map.get("impact")).isPresent()) {
            aSwitch.setDropOpen(Integer.parseInt(map.get("impact").toString()));
        }
        if (OptionalBean.ofNullable(map.get("temperature")).isPresent()) {
            aSwitch.setDropOpen(Integer.parseInt(map.get("temperature").toString()));
        }
        if (OptionalBean.ofNullable(map.get("environTemp")).isPresent()) {
            aSwitch.setDropOpen(Integer.parseInt(map.get("environTemp").toString()));
        }
        if (OptionalBean.ofNullable(map.get("elevation")).isPresent()) {
            aSwitch.setDropOpen(Integer.parseInt(map.get("elevation").toString()));
        }
        switchService.insertOrUpdate(aSwitch);
        return JSONObject.toJSONString(map);
    }
}
